//
//  GoodViewController.m
//  Hero
//
//  Created by lanou3g on 16/5/5.
//  Copyright © 2016年 刘永庆. All rights reserved.
//

#import "GoodViewController.h"
#import "GoodModel.h"
#import "GoodCollectionViewCell.h"
#import "GoodDetailViewController.h"
@interface GoodViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@end
#warning ----可以增加一个searchBar----------
@implementation GoodViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"物品列表";
    self.dataArray = [NSMutableArray new];
    self.view.backgroundColor = [UIColor whiteColor];
    UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
    self.collectionView = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
    layout.itemSize = CGSizeMake(70, 120);
    [self.view addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"GoodCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"goodCell"];
    [self reloadDate];
}
- (void)reloadDate
{
    [YQNetWorkRequestManager requestURL:[NSURL URLWithString:@"http://ossweb-img.qq.com/upload/qqtalk/lol_hero/goods_list.js"] method:YQ_GET params:nil result:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data) {
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            for (NSDictionary *dic in dict[@"items"]) {
                GoodModel *model = [GoodModel new];
                [model setValuesForKeysWithDictionary:dic];
                [self.dataArray addObject:model];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.collectionView reloadData];
            });
        }
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    GoodCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"goodCell" forIndexPath:indexPath];
    cell.model = self.dataArray[indexPath.row];
    
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    GoodDetailViewController *deVc  = [GoodDetailViewController new];
    GoodModel *model = self.dataArray[indexPath.row];
    deVc.good_id = model.good_id;
    [self.navigationController pushViewController:deVc animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
